﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using BotonJXC.BLL;
using BotonJXC.Entity;
using NHibernate;
using BotonJXC.DAO;

namespace BotonJXC.Forms.Controls
{
    public partial class LogisticsModelControl : BaseInfoControl
    {
        LogisticsModelBLL _logisticsModelBll = new LogisticsModelBLL();
        string _logisticsModelName;
        public LogisticsModelControl()
        {
            InitializeComponent();
        }

        private void kbSearch_Click(object sender, EventArgs e)
        {
            _logisticsModelName = ktbLogisticsModelName.Text;
            pager1.PageNo = 1;
            LoadData();
        }

        private void LogisticsModelControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            PageModel<LogisticsModel> pageModel = _logisticsModelBll.Find(_logisticsModelName, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            kdgvLogisticsModel.DataSource = new BindingList<LogisticsModel>(pageModel.Results);
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvLogisticsModel.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void AddInfo()
        {

        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvLogisticsModel.Rows)
                {
                    LogisticsModel u = r.DataBoundItem as LogisticsModel;
                    if (u == null)
                        continue;
                    if (u.LogisticsModelId == 0)
                    {
                        _logisticsModelBll.Save(u);
                    }
                    else if ("1".Equals(r.Tag) && u.LogisticsModelId != 0)
                    {
                        _logisticsModelBll.Update(u);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void DeleteInfo()
        {
            IList<LogisticsModel> logisticsModels = (IList<LogisticsModel>)kdgvLogisticsModel.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvLogisticsModel.SelectedRows)
                {
                    LogisticsModel item = r.DataBoundItem as LogisticsModel;
                    if (item != null)
                    {
                        _logisticsModelBll.Delete(item);
                    }
                    logisticsModels.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvLogisticsModel.Refresh();
        }

        private void kdgvLogisticsModel_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvLogisticsModel.Rows[e.RowIndex].Tag = "1";
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
